package controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import model.Page;
import DAO.LogDao;
import DAO.PageDao;
import database.DBManager;

public class Logger {
	
	public static DBManager myDb;
	
	public Logger(DBManager myDb){
		this.myDb = myDb;
	}
	
	public static void createLog(HttpServletRequest request){
		HttpSession session = request.getSession();
		int userID = Integer.parseInt(session.getAttribute("klantnummer").toString());
		PageDao pDao = new PageDao(myDb);
		Page page = pDao.getPageByPath(request.getRequestURL().toString());
		if(page == null){
			pDao.createPage(request.getRequestURL().toString());
			page = pDao.getPageByPath(request.getRequestURL().toString());
		}
		int pageID = 0;
		if(page != null){
			pageID = page.getPageID();
		}
		LogDao ldao = new LogDao(myDb);
		String IPaddress = request.getRemoteAddr();
		ldao.createLog(userID, pageID, IPaddress);
	}

}
